package com.android.view.activity.user;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.R;
import com.android.model.info.LocalSp;
import com.android.model.db.TransformColorUtil;
import com.android.util.log.LogUtil;
import com.android.view.activity.base.ToolbarBaseActivity;

import butterknife.BindView;

public class ColorPreferenceActivity extends ToolbarBaseActivity {

    private Context context;

    @BindView(R.id.llRedUpsGreenDowns)
    LinearLayout llRedUpsGreenDowns;
    @BindView(R.id.llGreenUpsRedDowns)
    LinearLayout llGreenUpsRedDowns;

    @BindView(R.id.tvRedGreen)
    TextView tvRedGreen;
    @BindView(R.id.tvGreenRed)
    TextView tvGreenRed;

    @BindView(R.id.checkboxRedGreen)
    ImageView checkboxRedGreen;
    @BindView(R.id.checkboxGreenRed)
    ImageView checkboxGreenRed;

    private int redColor = Color.argb(255, 255, 46, 91);//红
    private int greenColor = Color.argb(255, 52, 224, 144);//绿
    private int defaultColor = Color.argb(255, 170, 170, 170);//灰色
    private int boxStatus;
    private String tvRedGreenString = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

//    @Override
//    public void onBackPressed() {
//        if (!StringUtils.isEmpty(tvRedGreenString)) {
//            Intent intent = new Intent();
//            intent.putExtra(CommonType.MYSELF_SETTING_BOX_STATUS, tvRedGreenString);
//            setResult(2, intent);
//            finish();
//        }
//    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_preference_color);
    }

    @Override
    protected void initView() {
        context = this;
        boxStatus = LocalSp.getInstance(context).getCheckColorStatus();
        LogUtil.e(boxStatus);
        switch (boxStatus) {
            case 0:
                checkboxRedGreen.setBackgroundResource(R.mipmap.color_preference_checked);
                checkboxGreenRed.setBackgroundResource(R.color.transparent);
                tvRedGreenString = tvRedGreen.getText().toString();
                break;
            case 1:
                checkboxRedGreen.setBackgroundResource(R.color.transparent);
                checkboxGreenRed.setBackgroundResource(R.mipmap.color_preference_checked);
                tvRedGreenString = tvGreenRed.getText().toString();
                break;
            default:
                //默认是绿涨红跌
                checkboxRedGreen.setBackgroundResource(R.color.transparent);
                checkboxGreenRed.setBackgroundResource(R.mipmap.color_preference_checked);
                tvRedGreenString = tvGreenRed.getText().toString();
        }


        TransformColorUtil transformColorUtil = new TransformColorUtil(this);
        transformColorUtil.colorStart();
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {
        llRedUpsGreenDowns.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkboxRedGreen.setBackgroundResource(R.mipmap.color_preference_checked);
                checkboxGreenRed.setBackgroundResource(R.color.transparent);
                tvRedGreenString = tvRedGreen.getText().toString();
                LocalSp.getInstance(context).saveCheckColorStatus(0);
                LocalSp.getInstance(context).saveBeginColor(redColor);
                LocalSp.getInstance(context).saveEndColor(greenColor);
                LocalSp.getInstance(context).saveDefaultColor(defaultColor);
            }
        });

        llGreenUpsRedDowns.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkboxRedGreen.setBackgroundResource(R.color.transparent);
                checkboxGreenRed.setBackgroundResource(R.mipmap.color_preference_checked);
                tvRedGreenString = tvGreenRed.getText().toString();
                LocalSp.getInstance(context).saveCheckColorStatus(1);
                LocalSp.getInstance(context).saveBeginColor(greenColor);
                LocalSp.getInstance(context).saveEndColor(redColor);
                LocalSp.getInstance(context).saveDefaultColor(defaultColor);
            }
        });

//        checkboxRedGreen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    checkboxGreenRed.setChecked(false);
//                    tvRedGreenString = tvRedGreen.getText().toString();
//                    LocalSp.getInstance(context).saveCheckColorStatus(0);
//                    LocalSp.getInstance(context).saveBeginColor(redColor);
//                    LocalSp.getInstance(context).saveEndColor(greenColor);
//                    LocalSp.getInstance(context).saveDefaultColor(defaultColor);
//                }
//            }
//        });
//
//        checkboxGreenRed.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    checkboxRedGreen.setChecked(false);
//                    tvRedGreenString = tvGreenRed.getText().toString();
//                    LocalSp.getInstance(context).saveCheckColorStatus(1);
//                    LocalSp.getInstance(context).saveBeginColor(greenColor);
//                    LocalSp.getInstance(context).saveEndColor(redColor);
//                    LocalSp.getInstance(context).saveDefaultColor(defaultColor);
//                }
//            }
//        });
    }
}
